<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item>信息流管理</el-breadcrumb-item>
      <el-breadcrumb-item>客户管理</el-breadcrumb-item>
    </el-breadcrumb>
    <Container class="u-mb15">
      <!-- 筛选 -->
      <el-form
        inline
        class="m-form--filter"
      >
        <el-form-item>
          <el-select
            class="u-w160"
            size="mini"
            filterable
            placeholder="请选择客户"
            v-model="filter.customerId"
          >
            <el-option
              v-for="item in customerOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select
            class="u-w160"
            size="mini"
            placeholder="请选择客户角色"
            v-model="filter.customerRoleId"
          >
            <el-option
              v-for="item in [{label: '全部客户角色', value: null}].concat(CUSTOMER_ROLE_OPTION)"
              :key="item.value"
              :value="item.value"
              :label="item.label"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select
            class="u-w160"
            size="mini"
            placeholder="请选择客户状态"
            v-model="filter.status"
          >
            <el-option
              v-for="item in [{label: '全部客户状态', value: null}].concat(CUSTOMER_STATUS_OPTION)"
              :key="item.value"
              :value="item.value"
              :label="item.label"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            size="mini"
            @click="search"
          >查询</el-button>
        </el-form-item>
      </el-form>
    </Container>

    <Container>
      <!-- 表格操作 -->
      <el-form
        inline
        class="m-table--handle"
      >
        <el-form-item>
          <el-button
            type="primary"
            size="mini"
            @click="$router.push({
              name: 'flow-customer-detail',
              query: {
                action: 'create'
              }
            })"
          >新增</el-button>
        </el-form-item>
         <el-form-item>
            <ImportTable :importName="importName" :importUrl="importUrl"></ImportTable>
          <!-- <el-upload
              with-credentials
              :action="`${BASE_URL}/manage/api/bus/customer/import_data`"
              :show-file-list="false"
              :on-success="contractUpload"
              :before-upload="contractBeforeUpload"
            >
            <el-button size="mini" type="primary">导入</el-button>
          </el-upload> -->
        </el-form-item>
        <el-form-item>
          <el-button
              @click="exportExcel('/manage/api/bus/customer/export',requestSearch)"
              size="mini"
            >导出</el-button>
        </el-form-item>
        <el-form-item>
          <el-link type="primary" :underline="false" @click="downloadTemplate">下载导入模版</el-link> 
        </el-form-item>
        <CustomTableColumn :columns.sync="tableColumn" />
      </el-form>
      <!-- 表格 -->
      <el-table
        border
        stripe
        :data="tableData"
      >
        <el-table-column
          align="center"
          v-for="(item, index) in filterTableColumn"
          :label="item.label"
          :key="index"
        >
          <template slot-scope="scope">
            <!-- 客户性质 -->
            <template v-if="item.prop === 'customerType'">
              {{mapOptionLabel(CUSTOMER_TYPE_OPTION, scope.row.customerType)}}
            </template>
            <!-- 客户角色 -->
            <template v-else-if="item.prop === 'customerRole'">
              {{mapOptionLabel(CUSTOMER_ROLE_OPTION, scope.row.customerRole)}}
            </template>
            <!-- 关联资质 -->
            <template v-else-if="item.prop === 'aptitudeCount'">
              <el-button
                v-if="scope.row.aptitudeCount > 0"
                type="text"
                @click="$router.push({
                  name: 'flow-customer-aptitude',
                  query: {
                    id: scope.row.id
                  }
                })"
              >{{scope.row.aptitudeCount}}</el-button>
              <template v-else>{{scope.row.aptitudeCount}}</template>
            </template>
            <!-- 是否合作 -->
            <template v-else-if="item.prop === 'status'">
              <el-switch
                :width="34"
                :value="scope.row.status"
                :active-value="2"
                :inactive-value="1"
                @change="changeStatus(scope.row.id, scope.row.status === 1 ? 2 : 1)"
              ></el-switch>
            </template>
            <!-- 其他 -->
            <template v-else>{{scope.row[item.prop]}}</template>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          width="150"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="$router.push({
                name: 'flow-customer-detail',
                query: {
                  action: 'edit',
                  id: scope.row.id
                }
              })"
            >编辑</el-button>
            <el-button
              type="text"
              @click="$router.push({
                name: 'flow-customer-detail',
                query: {
                  action: 'detail',
                  id: scope.row.id
                }
              })"
            >查看</el-button>
            <el-button
               v-if="scope.row.status == 1"
              type="text"
               @click="removeAptitude(scope.row.id)"
            >删除</el-button>
          </template>
        </el-table-column>
        
      </el-table>
      <!-- 分页 -->
      <el-pagination
        background
        :page-sizes="[50, 100, 500, 1000]"
        :page-size="page.pageSize"
        layout="total, prev, pager, next, sizes"
        :total="pageTotal"
        @size-change="handlePagerSizeChange"
        @current-change="handlePagerCurrentChange"
      />
    </Container>
     <!-- 导入 -->
      <el-dialog
        class="dialog"
        title="数据导入完成"
        :visible.sync="importVisible"
        width="450"
       >
        <div class="msg_suc">导入成功数据：{{importData.success}}条</div>
        <div class="msg_suc">导入失败数据：{{importData.fail}}条</div>
        <div v-for="(item,index) in importData.messages" class="msg" :key="index">
          <div class="msg_item">{{item}}</div>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button @click="importVisible = false">关 闭</el-button>
          <el-button type="primary" @click="importVisible = false">确 定</el-button>
        </span>
      </el-dialog>
  </div>
</template>

<script>
import Container from "@/components/container";
import ImportTable from "@/components/import-table.vue";
import CustomTableColumn from "@/components/custom-table-column";

import {
  CUSTOMER_TYPE_OPTION,
  CUSTOMER_ROLE_OPTION,
  CUSTOMER_STATUS_OPTION
} from "@/assets/js/flow-option";
import { mapOptionLabel,contractBeforeUpload } from "@/util";
import { exportExcel } from '@/utils/fileDown.js';

export default {
  name: "flow-customer",
  components: {
    Container,
    CustomTableColumn,
    ImportTable
  },
  data() {
    return {
      CUSTOMER_TYPE_OPTION,
      CUSTOMER_ROLE_OPTION,
      CUSTOMER_STATUS_OPTION,
      BASE_URL: process.env.VUE_APP_BASE_URL,
      // 客户名称
      customerOptions: [],
      //导入
      importVisible:false,
      importData:{
        fail:null,
        success:null,
        meaasges:null
      },
      importName:'导入',
      importUrl:'/manage/api/bus/customer/import_data',
      // 筛选
      filter: {
        customerId: null,
        customerRoleId: null,
        status: null
      },

      // 表格
      tableData: [],
      tableColumn: [
        {
          label: "客户名称",
          prop: "customerName",
          visible: true
        },
        {
          label: "客户性质",
          prop: "customerType",
          visible: true
        },
        {
          label: "客户角色",
          prop: "customerRole",
          visible: true
        },
        {
          label: "关联资质",
          prop: "aptitudeCount",
          visible: true
        },
        {
          label: "是否合作",
          prop: "status",
          visible: true
        }
      ],
      page: {
        pageNo: 1,
        pageSize: 50
      },
      pageTotal: 0
    };
  },
  computed: {
    requestSearch() {
      return {
        ...this.filter,
        ...this.page
      }
    },
    filterTableColumn() {
      return this.tableColumn.filter(item => item.visible);
    }
  },
  methods: {
    mapOptionLabel,
    contractBeforeUpload,
    exportExcel,
    
     contractUpload(res) {
      if (res.code === 0) {
        this.importVisible = true;
        this.importData = res.data
      } else {
        this.$message({
          type: "error",
          message: res.msg
        });
      }
      this.importLoading.close();
      this.getTable()
    },
    // 下载模板
    downloadTemplate() {
      location.href = "https://s.55duanzi.com/CMS/客户导入表格.xls";
    },
    // 删除资质
    removeAptitude(id) {
      this.$confirm("是否删除此账号？删除后不可恢复！", "删除", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$http({
            method: "post",
            url: "/manage/api/bus/customer/del",
            params: {
              id: id
            }
          }).then(res => {
            if (res.data.code === 0) {
              this.$message.success("删除成功!");
              this.getTable();
            } else {
              this.$message.error(res.data.msg);
            }
          });
        })
        .catch(() => {});
    },
    fetchCustomerOptions() {
      this.$http({
        method: "get",
        url: "/manage/api/bus/customer/get_customer_name",
        params: {
          status: 0
        }
      }).then(res => {
        if (res.data.code === 0) {
          this.customerOptions = [
            {
              id: null,
              name: "全部客户"
            }
          ].concat(res.data.data);
        }
      });
    },
    search() {
      this.page.pageNo = 1;
      this.getTable();
    },
    getTable() {
      this.$http({
        method: "post",
        url: "/manage/api/bus/customer/get_list",
        data: {
          ...this.filter,
          ...this.page
        }
      }).then(res => {
        if (res.data.code === 0) {
          let data = res.data.data;
          this.tableData = data.list;
          this.pageTotal = data.listCount;
        } else {
          this.$message.error(res.data.msg);
        }
      });
    },
    changeStatus(id, status) {
      this.$http({
        method: "post",
        url: "/manage/api/bus/customer/status",
        params: {
          id,
          status
        }
      }).then(res => {
        if (res.data.code === 0) {
          this.$message.success(`${status === 1 ? "停用" : "启用"}成功！`);
          this.getTable();
        } else {
          this.$message.error(res.data.msg);
        }
      });
    },
    handlePagerSizeChange(v) {
      this.page.pageSize = v;
      this.getTable();
    },
    handlePagerCurrentChange(v) {
      this.page.pageNo = v;
      this.getTable();
    }
  },
  activated(){
    this.getTable();
  },
  created() {
    this.fetchCustomerOptions();
  }
};
</script>
<style scoped lang="scss">
  .dialog{
    margin-left:100rpx;
    div{
      margin-bottom:10px;
    }
  }
  .msg{
    margin-left:50px;
  }
</style>